package com.banyuan.array;

import java.util.Arrays;

//冒泡
public class MaoPao {
    public static void main(String[] args) {
        int[] arr1 = {9, 6, 7, 4, 8, 1, 2, 3, 5};

        for (int i = 0; i < arr1.length - 1; i++) {
            for (int j = 0; j < arr1.length - 1 - i; j++) {

                int temp = arr1[j];

                if (arr1[j] > arr1[j + 1]) {
                    arr1[j] = arr1[j + 1];
                    arr1[j + 1] = temp;
                }
            }
        }

        System.out.println(Arrays.toString(arr1));

//选择
        int[] arr2 = new int[]{9, 6, 7, 4, 8, 1, 2, 3, 5};
        for (int i = 0; i < arr2.length - 1; i++) {
            for (int j = i + 1; j < arr2.length; j++) {
                if (arr2[i] > arr2[j]) {
                    int temp = arr2[i];
                    arr2[i] = arr2[j];
                    arr2[j] = temp;
                }
            }
        }

        System.out.println(Arrays.toString(arr2));
        //选择排序的标准版，每次比较不用交换，只用一个minindex来接受下标，内循环结束后交换
        int[] arr3 = new int[]{9, 6, 7, 4, 8, 1, 2, 3, 5};

        for (int i = 0; i < arr3.length - 1; i++) {
            int minindex = i;
            for (int j = i + 1; j < arr3.length; j++) {
                minindex = arr3[i] < arr3[j] ? i : j;
            }
            if (minindex != i) {
                int temp = arr3[i];
                arr3[i] = arr3[minindex];
                arr3[minindex] = temp;
            }
        }
        System.out.println(Arrays.toString(arr3));

        //插入排序法
        //自己写的一个整体排序的方法
        int[] arr4 = new int[]{9, 6, 7, 4, 8, 1, 2, 3, 5};
        for (int i = 1; i < arr4.length; i++) {
            //方法1
            /*int j = i;

            while (j > 0) {
                j--;
                if (arr4[j] < arr4[i]) {
                    j++;
                    break;
                }
            }*/

            /*int j = 0;

            while (j < i && arr4[j] < arr4[i]) {

                    j++;
                }
*/
            //方法2
            int j = 0;
            for (; j < i && arr4[j] < arr4[i]; j++) {
            }

            // System.out.println(j);
            int temp = arr4[i];//这一步非常的重要，因为在换值之后arr[i]的值会发生改变会向后yi yi w
          /*  int a = i;
            while (j < a) {
                arr4[a] = arr4[a - 1];
                a--;
            }*/

            System.arraycopy(arr4, j, arr4, j+1, i-j);
            arr4[j] = temp;
        }
        System.out.println("arr4的排列：");
        System.out.println(Arrays.toString(arr4));

        //传统的方法
        int[] arr5 = new int[]{9, 6, 7, 4, 8, 1, 2, 3, 5};
        for (int i = 1; i < arr5.length; i++) {
            int j = i - 1;
            int a = arr5[i];
            //
            System.out.println("下标是：" + i + "实际的元素是：" + a);
            //注意错误的写法
//            while (arr5[j] > arr5[i]) {
            //arr5[i]在第一次用arr[j + 1]赋完值之后就会因为就会变成了之前的一个数比较的数列就不存在了
            while (arr5[j] > a) {
                arr5[j + 1] = arr5[j];

                j--;
                if (j < 0) {
                    break;
                }
            }
//            System.out.println(arr5[i]);
            System.out.println("j的值是：" + j);
            arr5[j + 1] = a;
            for (int k = 0; k <= i; k++) {
                System.out.print(arr5[k] + " ");
            }
            System.out.println();
        }
        System.out.println(Arrays.toString(arr5));

    /*
    直插法排序的标准版
    * for (int i = 1; i < arr.length; i++) {
            int index = i;//箭头
            int num = arr[index];
            while(index >= 1 && num < arr[index-1]){
                arr[index] = arr[index-1];
                index--;
//                if(index == 0){
//                    break;
//                }
            }
            arr[index] = num;
        }*/
    }
}
